﻿using System;
using System.Collections.Generic;
using DotNetNeural.Metric;

namespace DotNetNeural.Utils.Adapters
{
    public class DeviationAdapter
    {
        public IDeviation Deviation { get; private set; }

        public DeviationAdapter()
            : this(new DefaultDeviation())
        {
        }

        public DeviationAdapter(IDeviation deviation)
        {
            if (deviation == null)
                throw new NullReferenceException("Illegal null-reference metric");

            Deviation = deviation;
        }

        /// <summary>
        /// Calculates mean distance using each pair of arrays in given collection
        /// </summary>
        public float CalculateMeanDistance(IEnumerable<Tuple<float[], float[]>> items)
        {
            if (items == null)
                throw new NullReferenceException("Illegal null-reference args");

            float result = 0f;

            foreach (var i in items)
            {
                result += Deviation.CalculateDeviation(i.Item1, i.Item2);
            }

            return result / 2;
        }
    }
}
